{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "85ef7be7",
   "metadata": {},
   "source": [
    "| **函数**                     | **描述**                                                                 | **代码示例**                                                                                   |\n",
    "|------------------------------|--------------------------------------------------------------------------|------------------------------------------------------------------------------------------------|\n",
    "| **初始化**                   | 创建 `TfidfVectorizer` 对象。                                            | ```python<br>vectorizer = TfidfVectorizer(max_features=1000, stop_words='english')```          |\n",
    "| **`fit`**                    | 拟合数据，学习词汇表和 IDF 值。                                          | ```python<br>vectorizer.fit(corpus)```                                                        |\n",
    "| **`transform`**              | 将文本数据转换为 TF-IDF 特征向量。                                       | ```python<br>X = vectorizer.transform(corpus)```                                               |\n",
    "| **`fit_transform`**          | 拟合并转换数据，一步完成。                                               | ```python<br>X = vectorizer.fit_transform(corpus)```                                           |\n",
    "| **`get_feature_names_out`**  | 获取词汇表（特征名称）。                                                 | ```python<br>vocab = vectorizer.get_feature_names_out()```                                     |\n",
    "| **`idf_`**                   | 获取 IDF 值。                                                            | ```python<br>idf = vectorizer.idf_```                                                         |\n",
    "| **`vocabulary_`**            | 获取词汇表及其索引。                                                     | ```python<br>vocab_dict = vectorizer.vocabulary_```                                            |\n",
    "| **`inverse_transform`**      | 将 TF-IDF 矩阵转换回原始文本（近似）。                                   | ```python<br>texts = vectorizer.inverse_transform(X)```                                        |\n",
    "| **`get_stop_words`**         | 获取停用词列表。                                                         | ```python<br>stop_words = vectorizer.get_stop_words()```                                       |\n",
    "| **`set_params`**             | 设置参数。                                                               | ```python<br>vectorizer.set_params(max_features=500)```                                        |\n",
    "| **`get_params`**             | 获取当前参数。                                                           | ```python<br>params = vectorizer.get_params()```                                               |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "54d69098",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "词汇表: ['document' 'second']\n",
      "TF-IDF 矩阵:\n",
      " [[1.         0.        ]\n",
      " [0.78722298 0.61666846]\n",
      " [0.         0.        ]\n",
      " [1.         0.        ]]\n",
      "IDF 值: [1.22314355 1.91629073]\n",
      "词汇表及其索引: {'document': 0, 'second': 1}\n"
     ]
    }
   ],
   "source": [
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "\n",
    "# 示例文本数据\n",
    "corpus = [\n",
    "    'This is the first document.',\n",
    "    'This document is the second document.',\n",
    "    'And this is the third one.',\n",
    "    'Is this the first document?'\n",
    "]\n",
    "\n",
    "# 初始化 TfidfVectorizer\n",
    "vectorizer = TfidfVectorizer(max_features=10, stop_words='english')\n",
    "\n",
    "# 拟合并转换数据\n",
    "X = vectorizer.fit_transform(corpus)\n",
    "\n",
    "# 获取词汇表\n",
    "vocab = vectorizer.get_feature_names_out()\n",
    "print(\"词汇表:\", vocab)\n",
    "\n",
    "# 查看 TF-IDF 矩阵\n",
    "tfidf_matrix = X.toarray()\n",
    "print(\"TF-IDF 矩阵:\\n\", tfidf_matrix)\n",
    "\n",
    "# 获取 IDF 值\n",
    "idf = vectorizer.idf_\n",
    "print(\"IDF 值:\", idf)\n",
    "\n",
    "# 获取词汇表及其索引\n",
    "vocab_dict = vectorizer.vocabulary_\n",
    "print(\"词汇表及其索引:\", vocab_dict)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
